package com.example.myapplication.tack_09;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.tack_09.adapter.ArrangeAdapter;
import com.example.myapplication.tack_09.adapter.FenLeiAdapter;
import com.example.myapplication.tack_09.data.ArrangeData;
import com.example.myapplication.tack_09.data.BannerData;
import com.example.myapplication.tack_09.data.FenLeiData;
import com.example.myapplication.utils.BannerTool;
import com.example.myapplication.utils.OkHttpTool;

import java.util.ArrayList;
import java.util.List;

public class LoveActivity extends AppCompatActivity {
    private ImageView exit;
    private List<BannerData.DataDTO> listBanner;
    private ViewPager banner;
    private List<FenLeiData.DataDTO> listFenLei;
    private RecyclerView fenLei;
    private List<ArrangeData.RowsDTO> listArrange;
    private RecyclerView arrangeData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_09_activity_love);
        init();
        exit_();
        //轮播图
        GetBanner();
        //分类
        getFenLei();
        //列表
        getArranger();
        //推荐公益

    }

    private void getArranger() {
        String url = "prod-api/api/public-welfare/public-welfare-activity/list?pageNum=1&pageSize=5";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ArrangeData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ArrangeData arrangeData = (ArrangeData)obj;
                if(arrangeData.getCode() ==200){
                    listArrange = arrangeData.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            arranger();
                        }
                    });
                }

            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void arranger() {
        ArrangeAdapter arrangeAdapter = new ArrangeAdapter(listArrange, LoveActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(LoveActivity.this);
        arrangeData.setAdapter(arrangeAdapter);
        arrangeData.setLayoutManager(linearLayoutManager);
    }

    private void getFenLei() {
        String url = "prod-api/api/public-welfare/public-welfare-type/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, FenLeiData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                FenLeiData fenLeiData = (FenLeiData)obj;
                if(fenLeiData.getCode() == 200){
                    listFenLei = fenLeiData.getData();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            FenLeiAdapter();
                        }
                    });
                }

            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void FenLeiAdapter() {
        FenLeiAdapter fenLeiAdapter = new FenLeiAdapter(listFenLei,LoveActivity.this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(LoveActivity.this,4);
        fenLei.setLayoutManager(gridLayoutManager);
        fenLei.setAdapter(fenLeiAdapter);
    }

    private void GetBanner() {
        String url = "prod-api/api/public-welfare/ad-banner/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, BannerData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                BannerData bannerData = (BannerData)obj;
                if(bannerData.getCode() == 200){
                    listBanner = bannerData.getData();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayList<String> list = new ArrayList<>();
                            for (BannerData.DataDTO dataDTO : listBanner) {
                                list.add(dataDTO.getImgUrl());
                            }
                            BannerTool bannerTool = new BannerTool(LoveActivity.this,list,banner);
                            banner.setAdapter(bannerTool);
                        }
                    });
                }

            }

            @Override
            public void onFailure(String msg) {

            }
        });

    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoveActivity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        banner = findViewById(R.id.banner);
        fenLei = findViewById(R.id.fenlei);
        arrangeData = findViewById(R.id.list);
    }
}